home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / program / wdj0797.zip / SHMIDT.ZIP / VW32DEMO.ASM < prev    next >
Assembly Source File  |  1996-11-14  |  7KB  |  247 lines

  1.    title vw32demo
  2.    page ,132
  3.  
  4. .386p
  5.  
  6. .xlist
  7.    include vmm.inc
  8.    include vwin32.inc
  9.    include debug.inc
  10.    include vw32svc.inc
  11.    include winerror.inc
  12. ; winerror.inc sets this to none, this conflicts with /Cx
  13. option casemap:notpublic    
  14. .list
  15.  
  16. W32STRUC     struc
  17.  W32T_SERVICE  dd ?  ; service #
  18.  W32T_HANDLER  dd ?  ; pointer to the previous handler address
  19.  W32T_HITCNT   dd ?  ; number of times this service got hit
  20. W32STRUC     ends
  21.  
  22. W32_THUNK    struc
  23.  W32T_Call   db 0E8h ;opcode for 'call imm'
  24.  W32T_Target dd 0    ;call target
  25.  W32T_Data   db (type W32STRUC) dup (?)
  26. W32_THUNK    ends
  27.  
  28. VxD_DATA_SEG
  29. HLIST dd 0       ; main linked list handle
  30. vwin32_int21h_handler   dd  0 ; original 002A0010h handler
  31.  
  32. VW32Demo_Major  equ 1
  33. VW32Demo_Minor  equ 0
  34.  
  35. ; Win32 DeviceIOControl dispatch table
  36. align 4
  37. ioctldisp   label   dword
  38.    dd  offset32 ioctl_openclose
  39.    dd  offset32 ioctl_openclose
  40.    dd  offset32 ioctl_HitCount
  41.    dd  offset32 ioctl_RmDir
  42. ioctlcnt equ ($-ioctldisp)/4
  43. ; Win32 service table
  44. Create_Win32_Services=1
  45. Begin_Win32_Services VW32DEMO
  46. Local_Win32_Service VW32DEMO, VW32Demo_GetObfuscator, 1
  47. End_Win32_Services VW32DEMO
  48. VxD_DATA_ENDS
  49.  
  50. Declare_Virtual_Device VW32DEMO, \
  51.                        VW32Demo_Major, \
  52.                        VW32Demo_Minor, \
  53.                        VW32Demo_Control
  54.  
  55. VxD_LOCKED_CODE_SEG
  56.  
  57. VW32Demo_Control proc
  58. Control_Dispatch SYS_DYNAMIC_DEVICE_INIT, VW32Demo_DynamicInit
  59. Control_Dispatch SYS_DYNAMIC_DEVICE_EXIT, VW32Demo_DynamicExit
  60. Control_Dispatch W32_DEVICEIOCONTROL, VW32Demo_W32dioc
  61.    clc
  62.    ret
  63. VW32Demo_Control endp
  64.  
  65. VW32Demo_DynamicInit proc
  66.    ; main linked list
  67.    mov     eax, LF_Alloc_Error + LF_Async + LF_Use_Heap
  68.    mov     ecx, type W32_THUNK
  69.    VMMCall List_Create
  70.    .IF CARRY?
  71.       Trace_Out 'VW32Demo: Unable to create main linked list'
  72.    .ELSE
  73.       mov     [HLIST], esi
  74.  
  75.       ; hook all Win32 services that exist at the moment
  76.       cCall   _Enumerate_Win32_Services,\
  77.                          <<offset32 HookWin32Services>>
  78.  
  79.       ; register our own Win32 service table
  80.       VMMCall _Register_Win32_Services,<<offset32 VW32Demo_DDB>,\
  81.                             <offset32 VW32Demo_Win32_Services>>
  82.       clc
  83.    .ENDIF  
  84.    ret
  85. VW32Demo_DynamicInit endp
  86.  
  87. VW32Demo_DynamicExit proc
  88.    ; 'unregister' our Win32 services
  89.    VMMCall _Register_Win32_Services,<<offset32 VW32Demo_DDB>,0>
  90.     
  91.    ; unhook all win32 services
  92.    mov     esi, [HLIST]
  93.    VMMCall List_Get_First
  94.    .WHILE !ZERO?
  95.       push    eax
  96.       cCall   _Unhook_Win32_Service,\
  97.                 <[eax].W32T_Data.W32T_SERVICE,eax>
  98.       pop     eax
  99.       VMMCall List_Get_Next
  100.    .ENDW
  101.     
  102.    VMMCall List_Destroy
  103.    clc
  104.    ret
  105. VW32Demo_DynamicExit      endp
  106.  
  107. VW32Demo_W32dioc proc uses esi edi ebx ebp
  108.    ; get ioctl index
  109.    ; increment it for CLOSEHANDLE to start the table
  110.    mov   eax, [esi].dwIoControlCode
  111.    inc   eax
  112.    .IF eax >= ioctlcnt
  113.       ; not supported service number
  114.       mov   eax, ERROR_NOT_SUPPORTED
  115.       stc
  116.    .ELSE
  117.       ; call through the ioctl branch table    
  118.       mov     edi, [esi].lpvInBuffer
  119.       call    ioctldisp[eax*4]
  120.  
  121.       xor   eax, eax
  122.       clc
  123.    .ENDIF
  124.    ret
  125. VW32Demo_W32dioc  endp
  126.  
  127. ; deviceIOcontrol services
  128. ioctl_openclose proc
  129.    ret
  130. ioctl_openclose endp
  131.  
  132. ioctl_HitCount proc
  133.    mov     esi, [HLIST]
  134.    .IF [edi]
  135.       mov     eax, [edi]
  136.       VMMCall List_Get_Next
  137.    .ELSE
  138.       VMMCall List_Get_First  
  139.    .ENDIF
  140.    .IF !ZERO?
  141.       mov     [edi], eax
  142.       push    [eax].W32T_Data.W32T_SERVICE
  143.       pop     [edi+4]
  144.       push    [eax].W32T_Data.W32T_HITCNT
  145.       pop     [edi+8]
  146.    .ELSE
  147.       mov     dword ptr [edi], 0
  148.    .ENDIF
  149.    ret
  150. ioctl_HitCount endp
  151.  
  152. ; hook VWIN32 Int21 service with the handler that
  153. ; simulates 'Unable to remove directory' function
  154. ioctl_RmDir proc
  155.    .IF ![vwin32_int21h_handler]
  156.       cCall   _Hook_Win32_Service,<002A0010h,\
  157.                        <offset32 VW32Demo_Int21hHandler>>
  158.       mov     [vwin32_int21h_handler], eax ; save original handler
  159.    .ELSE
  160.       cCall   _Unhook_Win32_Service,<002A0010h,\
  161.                        <offset32 VW32Demo_Int21hHandler>>
  162.       mov     [vwin32_int21h_handler], 0
  163.    .ENDIF
  164.    ret
  165. ioctl_RmDir endp
  166.  
  167. ; Enumerate_Win32_Services enumeration procedure
  168. HookWin32Services proc C,win32svc:dword
  169.    mov     esi, [HLIST]
  170.    ; service stub
  171.    VMMCall List_Allocate
  172.    .IF !CARRY?
  173.       mov     edx, offset32 GenericWin32Servicehandler
  174.       mov     [eax].W32T_Call, 0e8h       ; opcode for call
  175.       sub     edx,eax
  176.       sub     edx,5                       ; immediate
  177.       mov     [eax].W32T_Target, edx
  178.       mov     edx, eax        
  179.         
  180.       ; store win32 service number
  181.       push    win32svc
  182.       pop     [edx].W32T_Data.W32T_SERVICE
  183.  
  184.       ; hook win32 service
  185.       cCall   _Hook_Win32_Service,<win32svc,edx>
  186.  
  187.       ; store previous handler
  188.       mov     [edx].W32T_Data.W32T_HANDLER, eax
  189.  
  190.       ; initialize hit count
  191.       mov     [edx].W32T_Data.W32T_HITCNT, 0
  192.  
  193.       mov     eax, edx
  194.       VMMCall List_Attach_Tail
  195.    .ENDIF
  196.  
  197.    ; make _Enumerate_Win32_Services to continue for all services
  198.    xor     eax, eax
  199.    ret
  200. HookWin32Services endp
  201.  
  202. GenericWin32Servicehandler proc
  203.    pop     edx             ; points to W32 structure
  204.    inc     [edx].W32T_HITCNT
  205.    ; just jump to the next handler down the chain
  206.    mov     edx, [edx].W32T_HANDLER
  207.    jmp     [edx]
  208. GenericWin32Servicehandler endp
  209.  
  210. VW32Demo_Int21hHandler proc stdcall,pcrs:dword,
  211.                                     hvm:dword,
  212.                                     int21func:dword,
  213.                                     int21ECX:dword
  214.    ; monitor RmDir subfunction (713Ah)
  215.    .IF (int21func == 0713Ah)
  216.       ; emulate 'unable to remove directory'
  217.       ; by returning Carry set to the Win32 client
  218.       mov     edx, pcrs
  219.       or      [edx].Client_EFlags, CF_Mask
  220.    .ELSE
  221.       ; pass through all the other Int 21 requests
  222.       leave
  223.       mov     edx, [vwin32_int21h_handler]
  224.       jmp     [edx]
  225.    .ENDIF
  226.     
  227.    ret
  228. VW32Demo_Int21hHandler endp
  229.  
  230. ; Win32 service handler
  231. VW32Demo_GetObfuscator proc stdcall,pcrs:dword,
  232.                                     hvm:dword,
  233.                                     r3pid:dword
  234.    VxDCall VWIN32_GetCurrentProcessHandle
  235.    ; get the value of the obfuscator
  236.    xor     eax, r3pid
  237.    ; return obfuscator to the Ring3 caller
  238.    xchg    eax, pcrs
  239.    push    pcrs
  240.    pop     [eax].Client_EAX
  241.     
  242.    ret
  243. VW32Demo_GetObfuscator endp
  244.  
  245. VxD_LOCKED_CODE_ENDS
  246.    end
  247.